package com.im.service.metePay.smo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.im.MDA;
import com.im.service.dto.BaseException;
import com.im.service.dto.ImplementProject;
import com.im.service.dto.MetePay;
import com.im.service.dto.PageQuery;
import com.im.service.metePay.dao.IMetePayQueryDAO;
import com.im.service.model.MetePayItemVo;
import com.im.service.model.MetePayVo;
import com.im.service.projectInfo.smo.ProjectInfoQuerySMOImpl;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;

import net.sf.json.JSONObject;

/**
 *
 * 采购计划查询 SMO
 * @author wangxh
 * 018年3月27日
 */
@Service
public class MetePayQuerySMOImpl implements IMetePayQuerySMO {

  private static final Logger logger = LogManager.getLogger(ProjectInfoQuerySMOImpl.class);

  @Autowired
  private IMetePayQueryDAO metePayQueryDAO;


  @Override
	public PageQuery<MetePayItemVo> queryMetePayList(String inJson) throws Exception {

    try {
      if (!JsonUtil.jsonValidate(inJson)) {
        throw new BaseException(
                new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
      }
			PageQuery<MetePayItemVo> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
			MetePayItemVo metePay = JsonUtil.jsonToBean(inJson, MetePayItemVo.class);

      Map<String,Object> param = new HashMap<String,Object>();
      param.put("metePay", metePay);
      param.put("page", page);
      PageHelper.startPage(page.getPageNumber(), page.getPageSize());
			List<MetePayItemVo> list = metePayQueryDAO.queryMetePayList(param);
      page.setRows(list);
			page.setTotalPages(((Page<MetePayItemVo>) list).getPages());
			page.setTotal(((Page<MetePayItemVo>) list).getTotal());
      return page;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }

	public PageQuery<MetePayItemVo> queryMetePayStaticList(String inJson) throws Exception {

		try {
			if (!JsonUtil.jsonValidate(inJson)) {
				throw new BaseException(
						new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
			}
			PageQuery<MetePayItemVo> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
			MetePayItemVo metePay = JsonUtil.jsonToBean(inJson, MetePayItemVo.class);

			Map<String, Object> param = new HashMap<String, Object>();
			param.put("metePay", metePay);
			param.put("page", page);
			PageHelper.startPage(page.getPageNumber(), page.getPageSize());
			List<MetePayItemVo> list = metePayQueryDAO.queryMetePayStaticList(param);
			page.setRows(list);
			page.setTotalPages(((Page<MetePayItemVo>) list).getPages());
			page.setTotal(((Page<MetePayItemVo>) list).getTotal());
			return page;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			throw new Exception(ReturnsUtil.getStackTrackMessage(e));
		}
	}

	@Override
	public PageQuery<MetePayItemVo> queryMetePayStaticDetailList(String inJson)throws Exception {
		try {
			// 1.json格式校验
			if (!JsonUtil.jsonValidate(inJson)) {
				throw new BaseException(
						new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
			}
			
			PageQuery<MetePayItemVo> page = null;
			JSONObject jsonObj = JSONObject.fromObject(inJson);
			JsonUtil.getNotNullStringFromJSON(jsonObj, "contractId", "获取合同id时");
			if (jsonObj.containsKey(MDA.PAGE_QUERY_INFO)) {
				JSONObject pageInfo = jsonObj.getJSONObject(MDA.PAGE_QUERY_INFO);
				page = JsonUtil.jsonToBean(pageInfo.toString(), PageQuery.class);
				jsonObj.remove(MDA.PAGE_QUERY_INFO);
			}
			
			MetePayItemVo metePay = JsonUtil.jsonToBean(jsonObj.toString(), MetePayItemVo.class);

			Map<String,Object> param = new HashMap<String,Object>();
			param.put("metePay", metePay);
			if(page != null){
				param.put("page", page);
				PageHelper.startPage(page.getPageNumber(), page.getPageSize());
			}
			List<MetePayItemVo> list = metePayQueryDAO.queryMetePayStaticDetailList(param);
			for(MetePayItemVo temp : list){
				String contractItemId = temp.getContractItemId();
				List<MetePayVo> metePayList = metePayQueryDAO.queryMetePayListByContItemId(contractItemId);
				temp.setMetePayList(metePayList);
			}
			
			if(page != null){
				page.setTotalPages(((Page<MetePayItemVo>)list).getPages());
				page.setTotal(((Page<MetePayItemVo>)list).getTotal());
			}else{
				page = new PageQuery<MetePayItemVo>();
			}
			page.setRows(list);
			
			return page;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			throw new Exception(ReturnsUtil.getStackTrackMessage(e));
		}
	}

  	@Override
	public MetePayItemVo queryMetePayDetail(MetePay req) throws Exception {

    try {
      Map<String,Object> param = new HashMap<String,Object>();
      param.put("metePay", req);
			MetePayItemVo metePayDetail = metePayQueryDAO.queryMetePayDetail(param);
      return metePayDetail;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }
}
